<template>
  <div>
    <el-form ref="form" :model="searchForm" :inline="true">
      <el-form-item>
        <el-input v-model="searchForm.name" placeholder="请输入要查询的角色"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchRoleInfo">搜索</el-button>
        <el-button type="primary" @click="dialogFormVisible=true">新建</el-button>
        <el-popconfirm title="确认要批量删除吗！！！！" @confirm="deleteRole">
          <el-button slot="reference" type="danger" :disabled="deleteAllBtnStu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border stripe style="width: 100%" :cell-style="{padding:'2px'}" @selection-change="selectionChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column label="角色名称" width="200" prop="name">
      </el-table-column>
      <el-table-column label="角色编码" width="200" prop="code">
      </el-table-column>
      <el-table-column label="描述" width="600" prop="remark">
      </el-table-column>
      <el-table-column label="状态" width="72">
        <template slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.statu===1 " type="success">正常</el-tag>
          <el-tag size="mini" v-else-if="scope.row.statu===0 " type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="375">
        <template slot-scope="scope">
          <el-button type="text">分配权限</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button type="text" @click="editRole(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认要删除吗！！！！" @confirm="deleteRole(scope.row.id)">
            <el-button slot="reference" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination layout="prev, sizes,pager, next, total, jumper " :total="total" :page-size="size"
                   :page-sizes="[5,10,15,20]" :current-page="current" style="margin-top: 20px">
    </el-pagination>

    <el-dialog title="角色信息" :visible.sync="dialogFormVisible" width="550px" :before-close="handleClose">
      <el-form :model="RoleForm" ref="RoleForm" label-width="100px" :rules="rules">
        <el-form-item label="角色名称" label-width="100px" prop="name">
          <el-input v-model="RoleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色编码" label-width="100px" prop="code">
          <el-input v-model="RoleForm.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="100px" prop="remark">
          <el-input v-model="RoleForm.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="100px">
          <el-radio-group v-model="RoleForm.statu">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('RoleForm')">保存用户</el-button>
        <el-button type="primary" @click="resetForm('RoleForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      RoleForm:{},
      total: 0,
      size: 10,
      current: 1,
      tableData: [],
      deleteAllBtnStu: true,
      dialogFormVisible: false,
      searchForm: {
        name: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入角色编码', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
        ]
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    searchRoleInfo(){

    },
    handleClose(){
      this.RoleForm={}
      this.dialogFormVisible=false
    },
    loadRoleList() {
      this.$axios.get('/system/role/list').then(response => {
        this.tableData = response.data.data.records
        this.total = response.data.data.total
        this.size = response.data.data.size
        this.current = response.data.data.current
      })
    },
    deleteRole(id) {
      this.$axios.post('/system/menu/delete', id).then(response => {
        this.$message({
          message: response.data.message,
          type: 'success',
          showClose: true,
          onClose: () => {
            this.loadMenuList()
          }
        });
      })
    },
    selectionChange(value) {
      this.selection = value;
      if (this.selection) {
        this.deleteAllBtnStu = (this.selection.length === 0)
      }
    },
    editRole(id) {
      this.$axios('/role/userInfo/' + id).then(response => {
        this.RoleForm = response.data.data
        this.dialogFormVisible = true
      })
    },
    submitForm(formName){
      this.$refs.RoleForm.validate(valid => {
        if (valid) {
          this.$axios.post('/system/role/' + (this.RoleForm.id ? 'update' : 'save'), this.RoleForm).then(response => {
            this.$message({
              message: response.data.message,
              type: 'success',
              // showClose: true,
              duration:1000,
              onClose: () => {
                this.loadRoleList()
                this.dialogFormVisible = false
                this.resetForm(formName)
              }
            })
          })
        }
      })
    },
    resetForm(){
      this.RoleForm = {}
      this.$refs.RoleForm.resetFields();
    }
  },
  created() {
    this.loadRoleList()
  },
  mounted() {
    this.loadRoleList()
  }
};
</script>

<style scoped>
.el-table__body {
  font-size: 14px;
}

.el-button:hover {
  color: #333
}

.el-popconfirm-reference {
  cursor: pointer;
}

.el-pagination {
  display: flex;
  justify-content: flex-end;
}

.el-tag {
  font-size: 12px;
  padding: 2px 6px;
}

.el-table-column--selection {
  text-align: center;
}

.el-avatar {
  border-radius: 50%;
}

.el-divider {
  margin: 0 6px;
}

/*上传头像*/
/*.avatar-uploader {*/
/*  border: 1px dashed #d9d9d9;*/
/*  border-radius: 6px;*/
/*  cursor: pointer;*/
/*  position: relative;*/
/*  overflow: hidden;*/
/*}*/

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-dialog {
  text-align: center;
}

/* Style the dialog header */
.el-dialog__header {
  background-color: #2f4050;
  color: #fff;
  border-radius: 4px 4px 0 0;
  border-bottom: none;
}

/* Style the dialog title */
.el-dialog__title {
  font-size: 22px;
  font-weight: bold;
  padding: 20px;
}

/* Style the form labels */
.el-form-item__label {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

/* Style the input fields */
.el-input__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
}

/* Style the radio buttons */


.el-radio-button {
  flex: 1;
}

.dialog-footer {
  padding: 20px;
  text-align: right;
  border-top: 1px solid #ebeef5;
  border-radius: 0 0 4px 4px;
}

.dialog-footer .el-button {
  margin-left: 10px;
  border-radius: 4px;
}

.dialog-footer .el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
}

.dialog-footer .el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.dialog-footer .el-button--primary:active {
  background-color: #3a8ee6;
  border-color: #3a8ee6;
}

.dialog-footer .el-button--default {
  color: #333;
  border-color: #dcdfe6;
}

.dialog-footer .el-button--default:hover {
  color: #409EFF;
  border-color: #409EFF;
}

</style>